//
//  CIBNCategoryMenuView.m
//  CIBN
//
//  Created by ZNGOO on 16/7/21.
//  Copyright © 2016年 韦儒健. All rights reserved.
//

#import "CIBNCategoryMenuView.h"
#import "CIBNHomeListModel.h"

@interface CIBNCategoryMenuView ()

@property (nonatomic,strong) NSArray * items;
@end

@implementation CIBNCategoryMenuView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width - 40, frame.size.height)];
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.delegate = self;
        [self addSubview:self.scrollView];
    }
    return self;
}- (void)setScrollViewItemsArray:(NSArray *)items{
    
    
    NSMutableArray * nameArr = [[NSMutableArray alloc] init];
    [nameArr addObject:@"推荐"];
    for (CIBNHomeListModel * model in items) {
    
//        NSLog(@"%@",model.Name);
        
        [nameArr addObject:model.Name];
    }
    
    
    self.items = nameArr;
    
    // 2. 计算ScrollView的最大宽度
    CGFloat W = 10.0;
    
    for (int index = 0; index < nameArr.count; index++) {
        
        CGFloat w = [ZJModelTool sizeWithText:nameArr[index] font:[UIFont systemFontOfSize:14] height: self.bounds.size.height] + 10;
        
        // 1. 菜单
        UILabel * item = [[UILabel alloc] initWithFrame:CGRectMake(W, 0, w, self.bounds.size.height-1)];
        item.text = nameArr[index];
        item.textAlignment = NSTextAlignmentCenter;
        item.font = [UIFont systemFontOfSize:14];
        item.textColor = RGB(28, 32, 41);
        item.tag = index;
        item.userInteractionEnabled = YES;
        [self.scrollView addSubview:item];
        
        
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapItem:)];
        [item addGestureRecognizer:tap];
        
        
        // 2. 下划线
        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(W, self.bounds.size.height-1, w, 1)];
        image.backgroundColor = RGB(18, 146, 228);
        image.tag = index + 10;
        [self.scrollView addSubview:image];
        if (index != 0) {
            image.hidden = YES;
        }
        
        W = W + w + 10 ;
    }
    
    
    self.scrollView.contentSize = CGSizeMake(W, self.bounds.size.height);
    
}
#pragma mark - 点击
- (void)tapItem:(UITapGestureRecognizer *)tap{
        for (int index = 0; index < self.items.count; index++) {
            UIImageView * image = (UIImageView *)[self viewWithTag:index + 10];
            image.hidden = YES;
        }
    
        [UIView animateWithDuration:0.2 animations:^{
            UIImageView * image = (UIImageView *)[self viewWithTag:tap.view.tag + 10];
            image.hidden = NO;
        }];
    
    // 代理设置
    if([self.delegate respondsToSelector:@selector(didSelectItemIndex:)]){
        [self.delegate didSelectItemIndex:tap.view.tag];
    }
}


@end
